/*
 * Copyright (c) 2008-2012
 * LANIT
 * All rights reserved.
 *
 * This product and related documentation are protected by copyright and
 * distributed under licenses restricting its use, copying, distribution, and
 * decompilation. No part of this product or related documentation may be
 * reproduced in any form by any means without prior written authorization of
 * LANIT and its licensors, if any.
 *
 * $Id$
 */
package ru.lanit.bpm.ipa.timesheet;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import ru.lanit.bpm.ipa.timesheet.dao.EmployeeDao;
import ru.lanit.bpm.ipa.timesheet.dao.JdbcEmployeeDao;
import ru.lanit.commons.jdbc.DataSourceFactory;
import ru.lanit.commons.jdbc.JdbcTemplateExtended;

import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;

public class Bootstrap {
    private static final Logger logger = LoggerFactory.getLogger(Bootstrap.class);

    public static void main(String[] args) throws Exception {
        // parse args
        /*  args[0]: -settings
        *  args[1]: <settingsPath> (ex. C:/settings.xml)
        *  args[2]: -mrp
        *  args[3]: <mrpPath> (ex. C:/mrpNovember2012.csv)
        *  args[4]: -employeeToMail
        *  args[5]: <employeeToMailPath>
        *
        *
        */
        String settingsPath;
        String mrpPath;
        String employeeToMailPath;
        if (args.length == 0) {
            settingsPath = "src/setting/settings.xml";
            mrpPath = "src/test/resources/ru/lanit/bpm/ipa/timesheet/000-full.csv";
            employeeToMailPath = "src/test/resources/ru/lanit/bpm/ipa/timesheet/004-employee-to-mail-full.csv";
        } else if (args.length == 6) {
            settingsPath = args[1];
            mrpPath = args[3];
            employeeToMailPath = args[5];
        } else {
            throw new Exception("Input parameters");
        }

        Settings settings = (Settings) new XStream(new XppDriver()).fromXML(new BufferedReader(new FileReader(settingsPath)));
        Reader readerMrpPath = new BufferedReader(new FileReader(mrpPath));
        Reader readerEmployeeToMailPath = new BufferedReader(new FileReader(employeeToMailPath));
        DataSource dataSource = DataSourceFactory.create(settings.getDataSourceDriver(), settings.getDataSourceSubProtocol(),
                settings.getDataSourceIp(), settings.getDataSourcePort(), settings.getDataSourceDBName(), settings.getDataSourceUser(),
                settings.getDataSourcePassword(), settings.isDataSourceWrapIntoTransactionProxy(), settings.isDataSourceUseConnectionPool());
        PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
        EmployeeDao employeeDao = new JdbcEmployeeDao(new JdbcTemplateExtended(dataSource));
        PrepareRequisitesEmployee prepareRequisitesEmployee = new PrepareRequisitesEmployee(readerEmployeeToMailPath);
        DictionaryUpdater dictionaryUpdater = new DictionaryUpdater(employeeDao, settings, readerMrpPath, prepareRequisitesEmployee.getRequisites());
        TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition());


        try {
            dictionaryUpdater.updateDictionaries();
            transactionManager.commit(transaction);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
                transactionManager.rollback(transaction);
            } catch (Exception e1) {
                logger.error(e1.getMessage(), e1);
            }
            throw e;
        }
    }
}
